home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / Telephone Manager / Stiletto Sources / Sources / Utilities.c < prev   
Encoding:
C/C++ Source or Header  |  1995-07-06  |  13.6 KB  |  609 lines  |  [TEXT/MPS ]

  1. /************************************************************************************************/
  2. /*                                                                                                */
  3. /*    Program Name:    Stiletto                                                                    */
  4. /*                                                                                                */
  5. /*    File Name:        Utilities.c                                                                    */
  6. /*                                                                                                */
  7. /*    © Apple Computer, Inc. 1991-1995                                                            */
  8. /*    All Rights Reserved                                                                            */
  9. /*                                                                                                */
  10. /*    Revision History:                                                                            */
  11. /*                                                                                                */
  12. /*        Date        Who                    Modification                                            */
  13. /*                                                                                                */
  14. /*        1991-06-30    Chris Halim            Original version                                        */
  15. /*        1995-06-26    Jaakko Railo        Version 2.0                                                */
  16. /*                                                                                                */
  17. /************************************************************************************************/
  18.  
  19. /****************************************** DESCRIPTION ******************************************
  20.  
  21. *************************************************************************************************/
  22.  
  23. /******************************************** HEADERS *******************************************/
  24.  
  25. #include "Aliases.h"
  26. #include "Files.h"
  27. #include "Folders.h"
  28. #include "Fonts.h"
  29. #include "Gestalt.h"
  30. #include "Resources.h"
  31. #include "string.h"
  32. #include "TextUtils.h"
  33.  
  34. #include "Constants.h"
  35. #include "Utilities.h"
  36.  
  37. /****************************************** DEFINITIONS *****************************************/
  38.  
  39. const    char * nullString = "Nil";
  40.  
  41. /****************************************** PROTOTYPES ******************************************/
  42.  
  43. /******************************************** GLOBALS *******************************************/
  44.  
  45. extern    ModalFilterUPP    gAlertUserFilterUPP;
  46. extern    UserItemUPP        gDrawDefaultUPP;
  47.  
  48. /************************************************************************************************/
  49. /************************************************************************************************/
  50.  
  51.  
  52. #pragma segment Utilities
  53. Boolean IsAlias (Str255 fileName, short vRefNum, long dirID)
  54. {
  55.     /**
  56.      **        IsAlias() returns true if the file specified by fileName, vRefNum & dirID
  57.      **        is an alias. Otherwise it will return false.
  58.      **
  59.      **/
  60.      
  61.     FInfo    theFInfo;
  62.     OSErr    errCode;
  63.     FSSpec    theFileSpec;
  64.     
  65.     FSMakeFSSpec (vRefNum, dirID, fileName, &theFileSpec);
  66.     
  67.     errCode = FSpGetFInfo (&theFileSpec, &theFInfo);
  68.     if (errCode != noErr) {
  69.         AlertUser ("\pGetFInfo failed !", errCode);
  70.         return (false);
  71.     }
  72.     
  73.     return ((theFInfo.fdFlags & 0x8000) != 0);    // IM VI, p.9-29, bit 15 -> alias flag
  74. }
  75.  
  76.  
  77. #pragma segment Utilities
  78. pascal Boolean AlertUserFilterProc(DialogPtr theDialog, EventRecord * theEvent, short * itemHit)
  79. {
  80.     Boolean                wasHandled = false;
  81.     char                key;
  82.             
  83.     switch (theEvent->what) {
  84.         case keyDown :
  85.             key = theEvent->message & charCodeMask;
  86.             if ( theEvent->modifiers & cmdKey )            // Command key is down
  87.                 if (key == 'd') {
  88.                     *itemHit = kDebuggerButton;
  89.                     HiliteItem (theDialog, *itemHit, 1);
  90.                     wasHandled = true;
  91.                     break;
  92.                 }
  93.                 switch (key) {
  94.                     case crKey :
  95.                     case enterKey :
  96.                         *itemHit = ok;
  97.                         HiliteItem (theDialog, *itemHit, 1);    // hilite the ok button
  98.                         wasHandled = true;
  99.                         break;
  100.                 }
  101.             break;             
  102.     }
  103.  
  104.     return (wasHandled);
  105. }
  106.  
  107.  
  108. #pragma segment Utilities
  109. void AlertUser (Str255 pString, short errCode)
  110. {
  111.     //
  112.     //    For future update: think of implementing STR#, instead of hardcoded err strings!
  113.     //
  114.     
  115.     short        itemHit;
  116.     Str255        str1;
  117.     DialogPtr    theDialog;
  118.     short         itemType;
  119.     Handle        item;
  120.     Rect        box;
  121.  
  122.     SetCursor(&qd.arrow);
  123.  
  124.     theDialog = GetNewDialog (rUserAlertDLOG, nil, (WindowPtr)(-1L));
  125.     if (theDialog == nil) {
  126.         DebugStr ("\pUnable to find DLOG resource : rUserAlertDLOG !");
  127.         return;
  128.     }
  129.     SetPort (theDialog);
  130.  
  131.     GetDItem(theDialog, kDefaultUserItem, &itemType, &item, &box);
  132.     SetDItem(theDialog, kDefaultUserItem, itemType, (Handle) gDrawDefaultUPP, &box);
  133.     
  134.     NumToString ((long) errCode, str1);
  135.     
  136.     ParamText (pString, str1, "\p", "\p");
  137.     
  138.     ShowWindow (theDialog);
  139.  
  140.     ModalDialog (gAlertUserFilterUPP, &itemHit);
  141.     
  142.     if (itemHit == kDebuggerButton)
  143.         DebugStr ("\p;sc;ip");
  144.     
  145.     DisposDialog (theDialog);
  146. }
  147.  
  148.  
  149. #pragma segment Utilities
  150. void    SetPopupValue (DialogPtr theDialog, short itemNumber, short theValue)
  151. {
  152.     short        itemKind;
  153.     Handle        itemHand;
  154.     Rect        itemRect;
  155.     
  156.     GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  157.     
  158.     SetCtlValue ((ControlHandle) itemHand, theValue);
  159. }
  160.  
  161.  
  162. #pragma segment Utilities
  163. short    GetPopupValue (DialogPtr theDialog, short itemNumber)
  164. {
  165.     short        itemKind;
  166.     Handle        itemHand;
  167.     Rect        itemRect;
  168.     
  169.     GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  170.     
  171.     return (GetCtlValue ((ControlHandle) itemHand));
  172. }
  173.  
  174.  
  175. #pragma segment Utilities
  176. void HiliteItem (DialogPtr theDialog, short itemNumber, short hiliteState)
  177. {
  178.     short        itemKind;
  179.     Handle        itemHand;
  180.     Rect        itemRect;
  181.     
  182.     GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  183.     
  184.     HiliteControl ((ControlHandle) itemHand, hiliteState);
  185. }
  186.  
  187.  
  188. #pragma segment Utilities
  189. void     BlinkItem (DialogPtr    theDialog, short itemNumber)
  190. {
  191.     short    itemKind;
  192.     Handle    itemHand;
  193.     Rect    itemRect;
  194.     long    finalTicks;
  195.     
  196.     GetDItem (theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  197.     
  198.     HiliteControl ((ControlHandle) itemHand, 1);
  199.     Delay (8, &finalTicks);
  200.     HiliteControl ((ControlHandle) itemHand, 0);
  201. }
  202.  
  203.  
  204. #pragma segment Utilities
  205. Boolean    ToggleCheckBox (DialogPtr theDialog, short itemNumber)
  206. {
  207.     short        itemKind;
  208.     Handle        itemHand;
  209.     Rect        itemRect;
  210.     short        tShort;
  211.  
  212.     GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  213.     
  214.     tShort = GetCtlValue ((ControlHandle) itemHand);
  215.     tShort = ((tShort) ? 0 : 1);
  216.     
  217.     SetCtlValue ((ControlHandle) itemHand, tShort);
  218.     
  219.     return ((tShort) ? true : false);
  220. }
  221.  
  222.  
  223. #pragma segment Utilities
  224. pascal void DrawDefault (DialogPtr theDialog, short itemNumber)
  225. {
  226.     short        itemKind;
  227.     Handle        itemHand;
  228.     Rect        itemRect;
  229.     PenState    penState;
  230.     
  231.     GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  232.     
  233.     GetPenState(&penState);
  234.     
  235.     PenSize(3,3);
  236.     PenPat(&(qd.black));
  237.     
  238.     FrameRoundRect(&itemRect,16,16);
  239.     
  240.     SetPenState(&penState);
  241. }
  242.  
  243.  
  244. #pragma segment Utilities
  245. pascal void DrawBox (DialogPtr theDialog, short itemNumber)
  246. {
  247.     short        itemKind;
  248.     Handle        itemHand;
  249.     Rect        itemRect;
  250.     PenState    penState;
  251.     
  252.     GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
  253.     
  254.     GetPenState(&penState);
  255.     
  256.     PenSize(1,1);
  257.     PenPat(&(qd.black));
  258.     
  259.     FrameRect(&itemRect);
  260.     
  261.     SetPenState(&penState);
  262. }
  263.  
  264.  
  265. #pragma segment Utilities
  266. void    CenterRect (const Rect * bounds, Rect * theRect)
  267. {
  268.     OffsetRect(theRect, - theRect->left, - theRect->top);
  269.  
  270.     OffsetRect(theRect, bounds->left + (bounds->right - bounds->left - theRect->right) / 2,
  271.          bounds->top + (bounds->bottom - bounds->top - theRect->bottom) / 2);
  272. }
  273.  
  274.  
  275. #pragma segment Utilities
  276. void    SetRectFromResource (Rect * theRect, short resID)
  277. {
  278.     Handle    tHandle = Get1Resource ('RECT', resID);
  279.     
  280.     BlockMove (*tHandle, theRect, sizeof (Rect));
  281.     
  282.     ReleaseResource (tHandle);
  283. }
  284.  
  285.  
  286. #pragma segment Utilities
  287. Boolean    OutOfBound (const Rect * theRect, const Rect * bounds)
  288. {
  289.     if ((theRect->top < bounds->top) || (theRect->top > bounds->bottom))
  290.         return (true);
  291.  
  292.     if (theRect->right < bounds->left)
  293.         return (true);
  294.  
  295.     if (theRect->left > bounds->right)
  296.         return (true);
  297.  
  298.     if ((theRect->bottom - theRect->top) > (bounds->bottom - bounds->top))
  299.         return (true);
  300.  
  301.     if ((theRect->right - theRect->left) > (bounds->right - bounds->left))
  302.         return (true);
  303.     
  304.     return (false);
  305. }
  306.  
  307.  
  308. #pragma    segment Utilities
  309. Boolean    ToolFileStillExists (ConstStr255Param toolName)
  310. {
  311.     short         errCode;
  312.     short        vRefNum;
  313.     long        dirID;
  314.     FSSpec        theSpec;
  315.     Boolean        targetIsFolder, wasAliased;
  316.  
  317.     errCode = FindFolder (kOnSystemDisk, kExtensionFolderType, kDontCreateFolder, &vRefNum, &dirID);
  318.     if (errCode != noErr) {
  319.         AlertUser ("\pFindFolder failed !", errCode);
  320.         return (false);
  321.     }
  322.  
  323.     FSMakeFSSpec (vRefNum, dirID, toolName, &theSpec);
  324.     
  325.     errCode = ResolveAliasFile (&theSpec, true, &targetIsFolder, &wasAliased);
  326.     if (errCode != noErr) {
  327.         AlertUser ("\pResolveAliasFile failed !", errCode);
  328.         return (false);
  329.     }
  330.  
  331.     if (targetIsFolder) {
  332.         AlertUser ("\pThis is really strange, dude ! The tool suddenly becomes a folder.", errCode);
  333.         return (false);
  334.     }
  335.     
  336.     return (true);
  337. }
  338.  
  339.  
  340. #pragma    segment Utilities
  341. VBLTask * InstallVBLTask (ProcPtr taskFunc, short ticks)
  342. {
  343.     OSErr    errCode;
  344.     VBLTask    *taskPtr;
  345.     VBLUPP    theVBLUPP;
  346.     
  347.     taskPtr = (VBLTask *) NewPtr (sizeof (VBLTask));
  348.     
  349.     if (taskPtr != nil) {
  350.         theVBLUPP = NewVBLProc (taskFunc);
  351.         if (theVBLUPP != NULL) {
  352.             taskPtr->qType = vType;
  353.             taskPtr->vblAddr = theVBLUPP;
  354.             taskPtr->vblCount = ticks;
  355.             taskPtr->vblPhase = 0;
  356.             
  357.             errCode = VInstall ((QElemPtr) taskPtr);
  358.             if (errCode != noErr) {
  359.                 DisposPtr ((Ptr) taskPtr);
  360.                 taskPtr = nil;
  361.             }
  362.         }
  363.     }
  364.     
  365.     return (taskPtr);
  366. }
  367.  
  368.  
  369. #pragma    segment Utilities
  370. void    RemoveVBLTask (VBLTask * taskPtr)
  371. {
  372.     VRemove ((QElemPtr) taskPtr);
  373.     DisposPtr ((Ptr) taskPtr);
  374. }
  375.  
  376.  
  377. #pragma segment Utilities
  378. long    GetAppFreeMem (void)
  379. {
  380.     Size        largestContig, grow;
  381.     
  382.     largestContig = MaxMem (&grow);
  383.     return (FreeMem ());
  384. }
  385.  
  386.  
  387. #pragma segment Utilities
  388. long    GetSysFreeMem (void)
  389. {
  390.     Size        largestContig, grow;
  391.     
  392.     largestContig = MaxMemSys (&grow);
  393.     return (FreeMemSys ());
  394. }
  395.  
  396.  
  397. #pragma segment Utilities
  398. void     SetCheckMark (short menuID, short theItem)
  399. {
  400.     MenuHandle    aMenuHandle = GetMenu (menuID);
  401.     
  402.     SetItemMark (aMenuHandle, theItem, checkMark);
  403. }
  404.  
  405. #pragma segment Utilities
  406. pascal Boolean StandardFilterProc (DialogPtr theDialog, EventRecord * theEvent, short * itemHit)
  407. {
  408.     Boolean                wasHandled = false;
  409.     char                key;
  410.             
  411.     switch (theEvent->what) {
  412.  
  413.         case keyDown :
  414.  
  415.             key = theEvent->message & charCodeMask;
  416.             
  417.             switch (key) {
  418.                 case '.' :
  419.                     if (theEvent->modifiers & cmdKey) {
  420.                         *itemHit = cancel;
  421.                         BlinkItem (theDialog, *itemHit);
  422.                         wasHandled = true;
  423.                     }
  424.                     break;
  425.                     
  426.                 case 0x1B :                                    // Esc
  427.                     *itemHit = cancel;
  428.                     BlinkItem (theDialog, *itemHit);
  429.                     wasHandled = true;
  430.                     break;
  431.                     
  432.                 case 0x0D :                                    // CR
  433.                 case 0x03 :                                    // Enter
  434.                     *itemHit = ok;
  435.                     BlinkItem (theDialog, *itemHit);
  436.                     wasHandled = true;
  437.                     break;
  438.             }
  439.     }
  440.     
  441.     return (wasHandled);
  442. }
  443.  
  444.  
  445. #pragma segment Utilities
  446. void    SelectPreviousCell (ListHandle theList)
  447. {
  448.     Cell        tCell;
  449.     
  450.     if (theList) {
  451.         SetPt (&tCell, 0, 0);
  452.         if (LGetSelect (true, &tCell, theList)) {
  453.             if (tCell.v != (*theList)->dataBounds.top) {
  454.                 LSetSelect (false, tCell, theList);            // deselect current cell
  455.                 --tCell.v;
  456.                 LSetSelect (true, tCell, theList);            // select next cell
  457.             }
  458.         }
  459.         else {
  460.             tCell.v = (*theList)->dataBounds.bottom - 1;
  461.             LSetSelect (true, tCell, theList);
  462.         }
  463.         
  464.         LAutoScroll (theList);
  465.     }
  466. }
  467.  
  468.  
  469. #pragma segment Utilities
  470. void    SelectNextCell (ListHandle theList)
  471. {
  472.     Cell        tCell;
  473.     
  474.     if (theList) {
  475.         SetPt (&tCell, 0, 0);
  476.         if (LGetSelect (true, &tCell, theList)) {
  477.             if (tCell.v != (*theList)->dataBounds.bottom - 1) {
  478.                 LSetSelect (false, tCell, theList);            // deselect current cell
  479.                 ++tCell.v;
  480.                 LSetSelect (true, tCell, theList);            // select next cell
  481.             }
  482.         }
  483.         else
  484.             LSetSelect (true, tCell, theList);
  485.         
  486.         LAutoScroll (theList);
  487.     }
  488. }
  489.  
  490.  
  491. #pragma segment Utilities
  492. void    ClearAllSelectedCells (ListHandle theList)
  493. {
  494.     Cell    tCell;
  495.     
  496.     if (theList) {
  497.         SetPt (&tCell, 0, 0);
  498.         while (LGetSelect (true, &tCell, theList)) {
  499.             LSetSelect (false, tCell, theList);
  500.         }
  501.     }
  502. }
  503.  
  504.  
  505. #pragma segment Utilities
  506. short GetQDVersion ()
  507. {
  508.     return ((GetGestaltResult (gestaltQuickdrawVersion) >> 8) & 0xFF);
  509. }
  510.  
  511.  
  512. #pragma segment Utilities
  513. Boolean HasColorQD ()
  514. {
  515.     return (((GetGestaltResult (gestaltQuickdrawVersion) >> 8) & 0xFF) != 0);
  516. }
  517.  
  518.  
  519. #pragma segment Utilities
  520. long    GetGestaltResult (OSType gestaltSelector)
  521. {
  522.     long    gestaltResult;
  523.     
  524.     if (Gestalt(gestaltSelector, &gestaltResult) == noErr)
  525.         return(gestaltResult);
  526.     else
  527.         return(0);
  528. }
  529.  
  530.  
  531. #pragma segment Utilities
  532. void    ZoomWindowInCurrentDevice (WindowPtr theWindow, short maxWidth, short maxHeight,
  533.             short zoomDirection, Boolean front)
  534. {
  535.      WindowPtr    savedPort;
  536.     Rect        contentRect, structureRect, deviceRect, newRect;
  537.     short        width, height, dx, dy;
  538.     GDHandle    maxDevice;
  539.  
  540.     GetPort (&savedPort);
  541.     SetPort (theWindow);
  542.     EraseRect (&theWindow->portRect);
  543.  
  544.     if ((zoomDirection == inZoomOut) && (GetQDVersion() > 0)) {
  545.         
  546.         contentRect      = (**((WindowPeek) theWindow)->contRgn).rgnBBox;
  547.         structureRect = (**((WindowPeek) theWindow)->strucRgn).rgnBBox;
  548.         
  549.         maxDevice       = GetMaxDevice (&contentRect);
  550.         deviceRect      = (*maxDevice)->gdRect;
  551.         if (maxDevice == GetMainDevice ())
  552.             deviceRect.top += GetMBarHeight ();
  553.  
  554.         deviceRect.left   += (contentRect.left - structureRect.left + 2);
  555.         deviceRect.top    += (contentRect.top - structureRect.top + 2);
  556.         deviceRect.right  -= (structureRect.right - contentRect.right + 2);
  557.         deviceRect.bottom -= (structureRect.bottom - contentRect.bottom + 2);
  558.         newRect = deviceRect;
  559.  
  560.         if (maxWidth)
  561.             if ((width = deviceRect.right - deviceRect.left) > maxWidth)
  562.                 newRect.right = (newRect.left = contentRect.left) + maxWidth;
  563.         if (maxHeight)
  564.             if ((height = deviceRect.bottom - deviceRect.top) > maxHeight)
  565.                 newRect.bottom = (newRect.top = contentRect.top) + maxHeight;
  566.         if ((dx = deviceRect.left - newRect.left) < 0)
  567.             if ((dx = deviceRect.right - newRect.right) > 0)
  568.                 dx = 0;
  569.         if ((dy = deviceRect.top - newRect.top) < 0)
  570.             if ((dy = deviceRect.bottom - newRect.bottom) > 0)
  571.                 dy = 0;
  572.         OffsetRect(&newRect, dx, dy);
  573.  
  574.         (*(WStateDataHandle)(((WindowPeek)theWindow)->dataHandle))->stdState = newRect;
  575.     }
  576.  
  577.     ZoomWindow (theWindow, zoomDirection, front);
  578.     SetPort (savedPort);
  579. }
  580.  
  581.  
  582. #pragma segment Utilities
  583. char *    ConvertToCString (char * s1, const char * s2)
  584. {
  585.     if (s2) {
  586.         strncpy (s1, s2+1, s2[0]);
  587.         s1[s2[0]] = '\0';
  588.     }
  589.     else
  590.         strcpy (s1, nullString);
  591.  
  592.     return (s1);
  593. }
  594.  
  595.  
  596. #pragma segment Utilities
  597. MenuHandle GetPopupMHandle (DialogPtr dialog, short resID)
  598. {
  599.     short itemKind;
  600.     Handle itemHand;
  601.     Rect itemRect;
  602.     
  603.     GetDItem (dialog, resID, &itemKind, &itemHand, &itemRect);
  604.     
  605.     return (MenuHandle) **((long**) (**((ControlHandle) itemHand)).contrlData);
  606. }
  607.  
  608.  
  609.